package com.hps.notifydemo.bank1.message;

import com.hps.notifydemo.bank1.entity.AccountPay;
import com.hps.notifydemo.bank1.model.AccountChangeEvent;
import com.hps.notifydemo.bank1.service.AccountInfoService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author heps
 * @date 2019/12/14 16:00
 */
@Component
@RocketMQMessageListener(consumerGroup = "consumer_group_notifymsg_bank1", topic = "topic_notifymsg")
public class NotifyMsgListener implements RocketMQListener<AccountPay> {

    @Autowired
    private AccountInfoService accountInfoService;

    @Override
    public void onMessage(AccountPay accountPay) {
        if ("success".equals(accountPay.getResult())) {
            AccountChangeEvent accountChangeEvent = new AccountChangeEvent();
            accountChangeEvent.setAccountNo(accountPay.getAccountNo());
            accountChangeEvent.setAmount(accountPay.getPayAmount());
            accountChangeEvent.setTxNo(accountPay.getId());
            accountInfoService.updateAccountBalance(accountChangeEvent);
        }
    }
}
